﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 制样结果服务
    /// </summary>
    public class SamplePreparationResultService : TaskSchedulingAppService, ISamplePreparationResultService
    {
        private readonly IRepository<SamplePreparationResult, string> _samplePreparationResultRepository;

        public SamplePreparationResultService(IRepository<SamplePreparationResult, string> samplePreparationResultRepository)
        {
            _samplePreparationResultRepository = samplePreparationResultRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<SamplePreparationResultDto>> GetListAsync(SamplePreparationResultInput input)
        {
            var iQuery = await _samplePreparationResultRepository.GetQueryableAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<SamplePreparationResult>, List<SamplePreparationResultDto>>(iQuery.ToList());

                return new PagedResultDto<SamplePreparationResultDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<SamplePreparationResult>, List<SamplePreparationResultDto>>(items.ToList());

                return new PagedResultDto<SamplePreparationResultDto>(totalCount, itemsDto);
            }
        }
    }
}
